//引入模块
const ws = require('nodejs-websocket')
const DS = require('./DataStruct')
const userAction = require('./UserAction')
const roomAction = require('./RoomAction')
const gameAction = require('./GameAction')
//创建服务器
const server = ws.createServer((connect) => {
    console.log('在线人数：' + server.connections.length)
    connect.on('text', (str) => {
        //收到消息时调用
        const msg = JSON.parse(str)

        // if (msg.RequestCode === DS.RequestCode.USER) {
        //     if (msg.ActionCode === DS.UserAction.LOGIN) {
        //         console.log(msg.data)
        //     }
        // }
        switch (msg.RequestCode) {
            //用户请求类型
            case DS.RequestCode.USER:
                switch (msg.ActionCode) {
                    case DS.UserAction.LOGIN:
                        userAction.Login(msg.data, connect)
                        break;
                    case DS.UserAction.SIGNUP:
                        userAction.SignUp(msg.data, connect)
                        break;
                    case DS.UserAction.GET_INFO:
                        break;
                    case DS.UserAction.UPDATE_INFO:
                        break;
                }
                break;
            //房间请求类型
            case DS.RequestCode.ROOM:
                console.log('我是房间模块')
                switch (msg.ActionCode) {
                    case DS.RoomAction.CREATE:
                        //创建房间
                        roomAction.Create(connect)
                        break;
                    case DS.RoomAction.LIST:
                        //获取房间列表
                        roomAction.List(connect)
                        break;
                    case DS.RoomAction.JOIN:
                        //加入房间
                        roomAction.Join(msg.data, connect)
                        break;
                    case DS.RoomAction.QUIT:
                        //退出房间
                        roomAction.Quit(connect)
                        break;
                    case DS.RoomAction.READY:
                        //准备
                        roomAction.Ready(connect)
                        break;
                    case DS.RoomAction.UNREADY:
                        //取消准备
                        roomAction.Unready(connect)
                        break;
                    case DS.RoomAction.START:
                        //开始游戏
                        roomAction.Start(connect)
                        break;
                }
                break;
            //游戏请求类型
            case DS.RequestCode.GAME:
                console.log('我是游戏模块')
                switch (msg.ActionCode) {
                    case DS.GameAction.MOVE:
                        //数据同步 移动
                        gameAction.Move(connect)
                        break;
                    case DS.GameAction.OVER:
                        //游戏结束
                        gameAction.Over(connect)
                        break;
                }
                break;
            //大厅请求类型
            case DS.RequestCode.HALL:
                console.log('我是大厅模块')
                break;
            //主动通知请求类型
            case DS.RequestCode.NOTICE:
                console.log('我是主动通知模块')
                break;
        }
    })
    connect.on('close', (code, reason) => {
        //连接断开时调用
        if (code) {
            if (connect.roomId) {
                roomAction.Quit(connect)
            }
        }
    })
    connect.on('error', (code, reason) => {
        //连接错误时调用
    })
}).listen(8888, () => {
    console.log('服务器启动成功')
    console.log("端口号：8888")
})
